﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CubicFWNet.Data.Models;
using CubicFWNet.Data.ViewModal;

namespace CubicFWNet.Data.SystemManager
{
    public class OrderRepository : RepositoryBase<Order>
    {
        public IQueryable<OrderInfo> GetList(DateTime start, DateTime end, int stage, int status, string customer, string saler, string number)
        {
            var result = from Ord in dbcontext.Orders
                         join Cus in dbcontext.Customers on Ord.CustomeId equals Cus.Id
                         join Sta in dbcontext.OrderStatuss on Ord.Status equals Sta.Id
                         join Ur in dbcontext.Users on Ord.UserId equals Ur.Id
                         where Ord.CreateTime >= start && Ord.CreateTime < end && (Ord.Stage == stage || stage == 0) && (Ord.Status == status || status == 0) && (Cus.Name.Contains(customer) || customer == "") && (Ord.Number.Contains(number) || number == "") && (Ur.Id.ToString()==saler || saler == "")
                         select new OrderInfo
                         {
                             Id = Ord.Id,
                             Number = Ord.Number,
                             CustomeId = Ord.CustomeId,
                             Status = Ord.Status,
                             StatusName = Sta.StatusName,
                             Stage = Ord.Stage,
                             InitPrice = Ord.InitPrice,
                             FinalPrice = Ord.FinalPrice,
                             UserId = Ord.UserId,
                             CreatorId = Ord.CreatorId,
                             CreatorName = Ord.CreatorName,
                             CreateTime = Ord.CreateTime,
                             Delivery = Ord.Delivery,
                             Memo = Ord.Memo,
                             Channel = Ord.Channel,
                             CustomerName = Cus.Name,
                             SalerName = Ur.RealName,
                             CusNumber=Ord.CusNumber,
                         };
            return result;
        }

        public IQueryable<OrderInfo> GetInfo(Guid orderID)
        {
            var result = from Ord in dbcontext.Orders
                         join Cus in dbcontext.Customers on Ord.CustomeId equals Cus.Id
                         join Sta in dbcontext.OrderStatuss on Ord.Status equals Sta.Id
                         join Ur in dbcontext.Users on Ord.UserId equals Ur.Id
                         where Ord.Id==orderID
                         select new OrderInfo
                         {
                             Id = Ord.Id,
                             Number = Ord.Number,
                             CustomeId = Ord.CustomeId,
                             Status = Ord.Status,
                             StatusName = Sta.StatusName,
                             Stage = Ord.Stage,
                             InitPrice = Ord.InitPrice,
                             FinalPrice = Ord.FinalPrice,
                             UserId = Ord.UserId,
                             CreatorId = Ord.CreatorId,
                             CreatorName = Ord.CreatorName,
                             CreateTime = Ord.CreateTime,
                             Delivery = Ord.Delivery,
                             Memo = Ord.Memo,
                             Channel = Ord.Channel,
                             CustomerName = Cus.Name,
                             SalerName = Ur.RealName,
                             SendTime=Ord.SendTime,
                             CusNumber = Ord.CusNumber,
                         };
            return result;
        }

        public IQueryable<OrderInfo> GetListByProduct(Guid pid)
        {
            var result = from Ord in dbcontext.Orders
                         join OP in dbcontext.OrderProducts on Ord.Id  equals OP.OrderId
                         join user in dbcontext.Users on Ord.UserId equals user.Id
                         where OP.ProductId == pid && (Ord.Stage == 3 || Ord.Stage == 10)
                         orderby Ord.Stage, Ord.CreateTime
                         select new OrderInfo
                         {
                             Id = Ord.Id,
                             Number = Ord.Number,
                             CustomeId = Ord.CustomeId,
                             Status = Ord.Status,
                             Stage = Ord.Stage,
                             InitPrice = Ord.InitPrice,
                             FinalPrice = Ord.FinalPrice,
                             UserId = Ord.UserId,
                             CreatorId = Ord.CreatorId,
                             CreatorName = Ord.CreatorName,
                             CreateTime = Ord.CreateTime,
                             Delivery = Ord.Delivery,
                             Memo = Ord.Memo,
                             Channel = Ord.Channel,
                             SalerName = user.RealName,
                             SendTime=Ord.SendTime,
                             CusNumber = Ord.CusNumber,

                         };
            return result;
        }
    }
}
